package com.primary.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.primary.controller.request.DistributeRequest;
import com.primary.entity.Bloodbanks;
import com.primary.entity.Distribute;
import com.primary.mappers.BloodbanksMapper;
import com.primary.mappers.DistributeMapper;
import com.primary.service.BloodbanksService;
import com.primary.service.DistributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("DistributeService")
public class DistributeServiceImpl implements DistributeService {
    @Autowired
    DistributeMapper distributeMapper;

    @Autowired
    BloodbanksMapper bloodbanksMapper;
    /**
     * 用于分页显示信息
     *
     * @param distributeRequest
     */
    @Override
    public PageInfo<Map<String, Object>> queryByPage(DistributeRequest distributeRequest) {
        PageHelper.startPage(distributeRequest.getPageNum(), distributeRequest.getPageSize());
        return new PageInfo<>(distributeMapper.listByPage(distributeRequest));
    }

    /**
     * 插入一条数据
     *
     * @param distribute
     * @return
     */
    @Override
    public int insertOne(Distribute distribute) {
        //根据传入的血液信息去查询并返回相应的信息
        Bloodbanks queryblood=bloodbanksMapper.queryOneByBloodInfoForDis(distribute.getBloodInfo());
        if(null == queryblood){
            return 0;
        }
        distribute.setBloodType(queryblood.getBloodType());
        distribute.setBloodUsage(queryblood.getDonosum());
        //血液发放之后，需要更改其库存状态为 已使用
        Bloodbanks bloodbanks=new Bloodbanks();
        bloodbanks.setBloodInfo(distribute.getBloodInfo());
        bloodbanks.setOutstoreTime(distribute.getUseTime());
        bloodbanks.setOptioner(distribute.getOptioner());
        int rows=bloodbanksMapper.updateOneForDistribute(bloodbanks);
        if(rows==0){
            return 0;
        }
        return distributeMapper.insertOne(distribute);
    }

    /**
     * 更新一条数据
     *
     * @param distribute
     * @return
     */
    @Override
    public int updateOneById(Distribute distribute) {
        return distributeMapper.updateOneById(distribute);
    }

    /**
     * 删除一条记录
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Integer id) {
        return distributeMapper.deleteById(id) > 0;
    }
}
